<template>
  <div class="goodsList-container">
    <div class="breadCrumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-card class="box-card">
      <!-- 顶部搜索 -->
      <div class="text item">
        <div class="inputBtn">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="queryInfo.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsListData"
            ></el-button>
          </el-input>
          <el-button type="primary" @click="$router.push('/addGoods')"
            >添加商品</el-button
          >
        </div>
      </div>
      <!-- 表格 -->
      <el-table :data="goodsList" stripe style="width: 100%" border>
        <el-table-column width="50" type="index" label="#"> </el-table-column>
        <el-table-column width="750" prop="goods_name" label="商品名称">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column width="120px" prop="" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="deleteGoods(scope.row.goods_id)"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 15, 40]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="goodsList.length"
          background
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getGoodsList, deleteGoods } from "@/api/goods";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      // 商品列表
      goodsList: [
        {
          goods_name: "",
          add_time: "",
          goods_id: "",
          goods_number: "",
          goods_price: "",
          goods_weight: "",
          is_promote: "",
          upd_time: "",
        },
      ],
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      // 分页
      currentPage1: 5,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: 4,
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getGoodsListData();
  },
  methods: {
    // 每页多少条
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getGoodsListData();
    },
    // 有多少页
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getGoodsListData();
    },
    // 获取商品列表
    async getGoodsListData() {
      const { data } = await getGoodsList(this.queryInfo);
      this.goodsList = data.data.goods;
    },
    // 删除商品
    async deleteGoods(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          console.log(id);
          const { data } = await deleteGoods(id);
          console.log(data);
          if (data.meta.status !== 200) {
            this.$message.error("删除失败");
          }
          this.$message.success("删除成功");
          this.getGoodsListData();
          console.log(data);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped lang='sass'>
.goodsList-container
  padding: 20px
  .breadCrumbs
    // 面包屑导航
    margin-bottom: 20px

  .input-with-select
    margin-top: 15px
    margin-right: 20px
    width: 348px

  .el-table
    margin-top: 20px
    margin-bottom: 20px
</style>
